/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.itembiaya;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author hendro
 */
public class ItemBiayaDAOImpl implements ItemBiayaDAO {

    public ItemBiayaDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    public List<ItemBiaya> list(int kodePosBiaya, String keySearch) throws Exception {
        List<ItemBiaya> list = new ArrayList<ItemBiaya>();
        String sql = "SELECT kodeItem,namaItem,kodePosBiaya,kodeSatuan FROM ppmk.itemBiaya WHERE kodePosBiaya=? AND namaItem LIKE ? ORDER BY kodeItem";
        List<Map> rows = ClassConnection.getJdbc().queryForList(sql, new Object[]{
                    kodePosBiaya,
                    "%" + keySearch + "%"
                });
        for (Map m : rows) {
            ItemBiaya item = new ItemBiaya();
            item.setKodeItem(ClassAntiNull.AntiNullInt(m.get("kodeItem")));
            item.setNamaItem(ClassAntiNull.AntiNullString(m.get("namaItem")));
            item.setKodePosBiaya(ClassAntiNull.AntiNullInt(m.get("kodePosBiaya")));
            item.setKodeSatuan(ClassAntiNull.AntiNullString(m.get("kodeSatuan")));
            list.add(item);
        }
        return list;
    }

    public ItemBiaya select(int id) throws Exception {
        String sql = "SELECT kodeItem,namaItem,kodePosBiaya,kodeSatuan FROM ppmk.itemBiaya WHERE " +
                "kodeItem=?";
        return (ItemBiaya) ClassConnection.getJdbc().queryForObject(sql, new Object[]{
                    id
                }, new RowMapper() {

            public Object mapRow(ResultSet rs, int arg1) throws SQLException {
                ItemBiaya item = new ItemBiaya();
                item.setKodeItem(rs.getInt("kodeItem"));
                item.setNamaItem(rs.getString("namaItem"));
                item.setKodePosBiaya(rs.getInt("kodePosBiaya"));
                item.setKodeSatuan(rs.getString("kodeSatuan"));
                return item;
            }
        });

    }
}
